package model;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Where the data of the program is stored.
 * 
 * @author Rylie Nelson, Eric Preston
 */

public class Database implements Serializable {
	
	/**
	 * Serial version ID
	 */
	private static final long serialVersionUID = -462034396637893269L;

	/**
	 * The list of conferences
	 */
	private List<Conference> conferences;
	
	/**
	 * The list of users
	 */
	private List<User> users;
	
	/**
	 * The list of papers
	 */
	private List<Paper> papers;

	/**
	 * The database constructor
	 */
	public Database() {
		conferences = new ArrayList<Conference>();
		users = new ArrayList<User>();
		papers = new ArrayList<Paper>();
		
		//parseData();
		//importDummyPapers();
	}
	
	/**
	 * This method parses the data and places the data into their appropriate data structures
	 */
	private void parseData() {
		File data = new File("the_data.txt");
		Scanner lineParser = null;
		try {
			lineParser = new Scanner(data);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		lineParser.useDelimiter("\n");
		
		while (lineParser.hasNext()) {
			String curLine = lineParser.next();
			Scanner fieldParser = new Scanner(curLine);
			fieldParser.useDelimiter(",");

			int curID = fieldParser.nextInt();
			String curFirst = fieldParser.next();
			curFirst = curFirst.substring(1, curFirst.length() - 1);
			String curLast = fieldParser.next();
			curLast = curLast.substring(1, curLast.length() - 1);
			String curEmail = fieldParser.next();
			curEmail = curEmail.substring(1, curEmail.length() - 1);
			User curUser = new User(curEmail, curID, curFirst, curLast);
			users.add(curUser);
			if (fieldParser.hasNextInt()) { //if true this user has a conf associated with it
				curID = fieldParser.nextInt();
				String curTitle = fieldParser.next();
				curTitle = curTitle.substring(1, curTitle.length() - 1);
				String curDesc = fieldParser.next();
				curDesc  = curDesc.substring(1, curDesc.length() - 1);
				Conference tmpConf = new Conference(curTitle, curID, curDesc);
				if (!conferences.contains(tmpConf)) { //if the conference list does not contain the conference we just found
					conferences.add(tmpConf);
				} else { //if it already contained the conference, we want to grab a reference to it
					tmpConf = conferences.get(conferences.indexOf(tmpConf));
				}
				int tmpRoleId = fieldParser.nextInt();
				if (tmpRoleId == 1) { //user is a program chair
					tmpConf.setPC(curUser);
				} else if (tmpRoleId == 2) { //user is a SPC
					tmpConf.addSPC(curUser);
				} else if (tmpRoleId == 4) { //user is a reviewer
					tmpConf.addReviewer(curUser);
				}
			}
		}
	}
	
	/**
	 * This method imports the sample papers that we have created for testing purposes
	 */
	
	private void importDummyPapers() {
		File file = new File("Papers/PAPUHS.csv");
		Scanner lineParser = null;
		
		try {
			lineParser = new Scanner(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		lineParser.useDelimiter("\n");
		
		lineParser.next(); //skip past first line
		
		while (lineParser.hasNext()) {
			String curLine = lineParser.next();
			Scanner fieldParser = new Scanner(curLine);
			fieldParser.useDelimiter(",");
			
			String filePath = fieldParser.next();
			int confID = fieldParser.nextInt();
			int paperID = fieldParser.nextInt();
			String username = fieldParser.next();
			boolean acceptanceStatus = fieldParser.nextBoolean();
			String paperTitle = fieldParser.next();
			String firstName = fieldParser.next();
			String lastName = fieldParser.next();
			String category = fieldParser.next();
			String keyword = fieldParser.next();
			String abStract = fieldParser.next();
			
			//find the right conference...
			
			Conference theConf = null;
			
			for (int i = 0; i < conferences.size(); i++) {
				if (conferences.get(i).getID() == confID) {
					theConf = conferences.get(i);
				}
			}
			
			//find the right user...
			
			User theUser = null;
			
			for (int i = 0; i < users.size(); i++) {
				if (users.get(i).getName().equals(username)) {
					theUser = users.get(i);
				}
			}
			
			File theFile = new File("Papers/" + filePath);
			
			Paper thePaper = new Paper(theFile, theUser, paperTitle, category, keyword, abStract);
			
			//if accepted mark as such
			
			thePaper.setAccepted(acceptanceStatus);
			
			theConf.getPapers().add(thePaper);
			
			papers.add(thePaper);
		}
		
	}
	
	/**
	 * Returns the list of conferences
	 * @return the list of conferences
	 */
	
	public List<Conference> getConferences() {
		return conferences;
	}
	
	/**
	 * Returns the list of users
	 * @return the list of users
	 */
	public List<User> getUsers() {
		return users;
	}
	
	/**
	 * Returns the list of papers
	 * @return the list of papers
	 */
	public List<Paper> getPapers() {
		return papers;
	}
	
	

}
